
package fr.mehfile.api.update.tasks;



import java.util.ArrayList;

import android.content.Context;
import fr.mehfile.api.parser.CurrentNews;
import fr.mehfile.api.parser.News;
import fr.mehfile.api.parser.Program;
import fr.mehfile.api.parser.Programs;
import fr.mehfile.api.parser.Schedule;
import fr.mehfile.api.parser.Schedules;
import fr.mehfile.api.parser.UpdateData;
import fr.mehfile.api.update.Tasks;
import fr.mehfile.api.update.TasksListener;
import fr.mehfile.api.update.settings.UpdateManagerSettings;
import fr.mehfile.view.utils.Utils;

public class UpdateDataValidatorTask extends Tasks {

    private static final String TAG = "UpdateDataValidatorTask";
    public static final String OLD_XML_FILE = "old xml file";
    private UpdateData data;

    /**
     * This class is used to validate the UpdateData.</br> All data not
     * mandatory are removed if there are not conformed. If it's a mandatory an
     * exception is created.</br> If it's ok or only not mandatory values are
     * not conformed, the listener is called on onComplete with the data on
     * return.</br> If it's not ok, the listener is called on onError with the
     * Exception.
     * 
     * @param listener
     * @param data
     */
    public UpdateDataValidatorTask(TasksListener listener, UpdateData data,UpdateManagerSettings updateManagerSettings, Context context) {
        super(Tasks.VALIDATOR_TASK_ID, listener);
        this.data = data;
    }

    @Override
    protected Object doInBackground(Object... params) {
        Object result = null;
        try {
            if (data == null) {
                throw new NullPointerException();
            }

            int xmlVersion = data.getXmlversion();
            if (xmlVersion < 0) {
                throw new Exception("Version of xml is negative");
            }
//            if(xmlVersion <= updateManagerSettings.getXmlVersion()){
//                throw new Exception(OLD_XML_FILE);
//            }

            if (data.getServerUrl() == null) {
                throw new Exception("serverUrl exception");
            }

            Programs programs= data.getPrograms();
            if(programs != null){
                ArrayList<Program> program = programs.getProgram();
                if (program != null)
                    if (program.size() >= 0) {
                        for (int i = 0; i < program.size(); i++) {
                            if (!checkCurrentProgram(program.get(i)))
                                program.remove(i);
                        }
                    }
            }
            
            News news= data.getNews();
            if(news != null){
                ArrayList<CurrentNews> currentNews = news.getCurrentNews();
                if (currentNews != null)
                    if (currentNews.size() >= 0) {
                        for (int i = 0; i < currentNews.size(); i++) {
                            if (!checkCurrentNews(currentNews.get(i)))
                                currentNews.remove(i);
                        }
                    }
            }

            result = data;
            Utils.debugger.logDebug(TAG, data.toString());
        } catch (Exception e) {
            result = e;
        }
        return result;
    }

    @Override
    protected void onPostExecute(Object result) {
        super.onPostExecute(result);

        if (result instanceof UpdateData) {
            getListener().onComplete(this, result);
        }
        if (result instanceof Exception) {
           
            getListener().onError(this, (Exception) result);
        }
    }

    private boolean checkCurrentProgram(Program program) {

        String date = program.getDate();
        if (date == null || "".equals(date))
            return false;


        String title = program.getTitle();
        if (title == null || "".equals(title))
            return false;

        Schedules schedules= program.getSchedules();
        if(schedules != null){
            ArrayList<Schedule> schedule = schedules.getSchedule();
            if (schedule != null)
                if (schedule.size() >= 0) {
                    for (int i = 0; i < schedule.size(); i++) {
                        if (!chechCurrentSchedule(schedule.get(i)))
                            return false;
                    }
                }
        }

        return true;
    }
    
    private boolean chechCurrentSchedule(Schedule schedule){
        
        String start_time = schedule.getStart_time();
        if (start_time == null || "".equals(start_time))
            return false;
        
        String title = schedule.getTitle();
        if (title == null || "".equals(title))
            return false;
        
        return true;
        
    }

    private boolean checkCurrentNews(CurrentNews currentNews) {

        String title = currentNews.getTitle();
        if (title == null || "".equals(title))
            return false;
        
        String content = currentNews.getContent();
        if (content == null || "".equals(content))
            return false;

        return true;
    }


}
